var express = require('express');
var router = express.Router();
let mongoose = require('mongoose');
// //class
// //建立class需要的集合模型对象
// let Classname = new mongoose.Schema({
//     name: String
//   })
//   //class删除集合模型对象
//   let classdelate = new mongoose.Schema({
//     name: String
//   }) 
//    //建立class与模型的关系
//  let classModel=mongoose.model('classModel', Classname, 'class');
//   //class删除模型关系
//   mongoose.model('classdelateModel', classdelate, 'class');
//   //class add模型
//   let classaddModel=mongoose.model('classaddModel', classdelate, 'class');
  //class 渲染操作
  router.post('/classshow', async function (req, res, next) {
    let resclass = await mongoose.model('classModel').find({});
    res.send(resclass);
    console.log(resclass);
  })
  //class新增
  router.post('/addclass',async function(req,res){
   //添加的数据
   let re=await classaddModel.create({
     name: req.body.name
   })
   res.send({
     mes:'添加成功'
   })
  })

  //class 删除操作
  // classdelate
  router.post('/classdelate', async function (req, res, next) {
    // console.log(req.body); //找到了要删除的那个班级名字
    // console.log(req.body._id);
    let re= await mongoose.model('classdelateModel').deleteMany({
      _id:mongoose.Types.ObjectId(req.body._id)  
    });
   if(re.deletedCount>0){
       res.send({
           mes:'删除成功'
       })
   }else{
    res.send({
        mes:'删除失败'
    })
   }
    //渲染
  })
  //classname修改
  router.post('/classchange', async function (req, res, next) {
    console.log(req.body);
await mongoose.model('classModel').updateMany({
        _id:req.body._id  
    },{
      name: req.body.name
    });
  
    res.send({
      code:200,
      mes: "修改成功"
    })
  
  })
  //暴露
  module.exports = router;
